package com.xunk.common.core.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.common.core.utils
 * @ClassName: ThreadLocalUtils
 * @Author: yangwendong
 * @Description: 线程局部变量工具类
 * @Date: 2021/5/17 17:05
 * @Version: 1.0
 */
public class ThreadLocalUtils {
    private ThreadLocalUtils() {
    }

    private static ThreadLocal<Map<String, Object>> t = new ThreadLocal<>();

    public static void put(String key, Object value) {
        Map<String, Object> cache = t.get();
        if (Objects.isNull(cache)) {
            cache = new HashMap<>();
        }
        cache.put(key, value);
        t.set(cache);
    }

    public static <T> T get(String key) {
        Map<String, Object> cache = t.get();
        if (Objects.isNull(cache)) {
            return null;
        }
        return (T) cache.get(key);
    }

    public static void clear() {
        t.remove();
    }
}
